//
//  CIDetailView.m
//  messageManager
//
//  Created by desunire on 2017/9/8.
//  Copyright © 2017年 desunire. All rights reserved.
//

#import "CIDetailView.h"

@implementation CIDetailView
{
    UILabel  *_title;//title
    UIView      * _listView;    // 下拉列表背景View
    UITableView * _tableView;   // 下拉列表
    CGFloat       _rowHeight;   // 下拉列表行高
    UIButton     *_closeBtn;
    NSInteger    _rowNumber;//显示行数
    MessageObject *_messageObject;
}
-(instancetype)initWithFrame:(CGRect)frame andObject:(MessageObject *)messageObject andRowHeight:(CGFloat)rowHeight{
    
    //全屏展示
    if (self = [super initWithFrame:frame]) {
        
        _rowHeight = rowHeight;
        
        _rowNumber = 7;
        
        _messageObject = messageObject;
        
        [self setUI];
        
    }
    return  self;
}


-(void)setUI{
    
    
    
    UIView *backView = [[UIView alloc] initWithFrame:self.frame];
    backView.backgroundColor = [UIColor blackColor];
    backView.alpha=0.3;
    [self addSubview:backView];
    
    // 下拉列表背景View
    _listView = [[UIView alloc] init];
    _listView.frame = CGRectMake(30, 40+64,self.frame.size.width-60,_rowNumber*_rowHeight+50+80);
    _listView.clipsToBounds       = YES;
    _listView.layer.masksToBounds = YES;
    _listView.backgroundColor=[UIColor whiteColor];
    _listView.layer.cornerRadius = 5;
    [self addSubview:_listView];
    
    
    //topView
    
    UIView *topView =[UIView new];
    
    topView.frame = CGRectMake(0, 0, _listView.frame.size.width, 60);
    
    topView.backgroundColor = navColor;
    
    [_listView addSubview:topView];
    
    _title = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, _listView.frame.size.width, 30)];
    
    _title.text = @"相关CI详情";
    
    _title.font = [UIFont boldSystemFontOfSize:20.0f];
    
    _title.textColor = [UIColor whiteColor];
    
    _title.textAlignment =NSTextAlignmentCenter;
    
    [_listView addSubview:_title];
    
    // 下拉列表TableView
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 80,_listView.frame.size.width-20,_rowNumber*_rowHeight)];
    _tableView.delegate        = self;
    _tableView.dataSource      = self;
    _tableView.separatorStyle  = UITableViewCellSeparatorStyleNone;
    _tableView.bounces         = NO;
    [_listView addSubview:_tableView];
    
    
    _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _closeBtn.frame= CGRectMake(_listView.frame.size.width-30, 10, 20, 20);
    [_closeBtn setImage:[UIImage imageNamed:@"del_w"] forState:UIControlStateNormal];
    [_closeBtn addTarget:self action:@selector(closeView) forControlEvents:UIControlEventTouchUpInside];
    [_listView addSubview:_closeBtn];
    
}


-(void)closeView{
    
    if ([self.delegate respondsToSelector:@selector(closeView)]) {
        [self.delegate closeView];
    }
}


#pragma mark tableView代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _rowNumber;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    //UITableViewCell *cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    ProcessitemTableViewCell *cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([ProcessitemTableViewCell class]) owner:self options:nil] lastObject];
    
    
    [self setCellWithIndex:indexPath.row andCell:cell];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
//    cell.textLabel.text = [self titleInCellWithIndex:indexPath.row];
    
    return cell;
}



//
-(void)setCellWithIndex:(NSInteger)index andCell:(ProcessitemTableViewCell *)cell{
    if (index==0) {
        [cell initCellWithTitle:@"全局变量:" andContent:[self titleInCellWithIndex:index]];
    }
    if (index==1) {
        [cell initCellWithTitle:@"设备名称:" andContent:[self titleInCellWithIndex:index]];
    }
    if (index==2) {
        [cell initCellWithTitle:@"设备厂商:" andContent:[self titleInCellWithIndex:index]];
    }
    if (index==3) {
        [cell initCellWithTitle:@"型号:" andContent:[self titleInCellWithIndex:index]];
    }
    if (index==4) {
        [cell initCellWithTitle:@"监控者:" andContent:[self titleInCellWithIndex:index]];
    }
    if (index==5) {
        [cell initCellWithTitle:@"DNS名称:" andContent:[self titleInCellWithIndex:index]];
    }
    if (index==6) {
        [cell initCellWithTitle:@"位置:" andContent:[self titleInCellWithIndex:index]];
    }
}

//针对特定需求的数据解析
-(NSString *)titleInCellWithIndex:(NSInteger)index{
    
    if (index==0) {
        return _messageObject.globalId?_messageObject.globalId:@"";
    }
    if (index==1) {
        return _messageObject.displayLabel?_messageObject.displayLabel:@"";
    }
    if (index==2) {
        return _messageObject.discoveredVendor?_messageObject.discoveredVendor:@"";
    }
    if (index==3) {
        return _messageObject.nodeModel?_messageObject.nodeModel:@"";
    }
    if (index==4) {
        return _messageObject.monitoredBy?_messageObject.monitoredBy:@"";
    }
    if (index==5) {
        return _messageObject.primaryDnsName?_messageObject.primaryDnsName:@"";
    }
    if (index==6) {
        return _messageObject.location?_messageObject.location:@"";
    }
    return @"";
}


@end
